package com.lagou.boot;

import com.alibaba.fastjson.JSONObject;
import com.lagou.client.RPCConsumer;
import com.lagou.service.IUserService;
import com.lagou.service.JSONSerializer;
import com.lagou.service.RpcRequest;

public class ConsumerBoot {

    //参数定义
    private static final String PROVIDER_NAME = "UserService#sayHello#";

    public static void main(String[] args) throws InterruptedException {
        //封装RpcRequest对象 传递数据到服务端
        RpcRequest rpcRequest=new RpcRequest();
        rpcRequest.setRequestId("com.lagou.service");//id
        rpcRequest.setClassName("UserServiceImpl");//类名
        rpcRequest.setMethodName("test");//方法名


        //序列化
        JSONSerializer jsonSerializer=new JSONSerializer();
        byte[] baBytes=jsonSerializer.serialize(rpcRequest);
        //byte数组转字符串便于传输
        String res = new String(baBytes);



        //1.创建代理对象
       IUserService service = (IUserService) RPCConsumer.createProxy(IUserService.class, PROVIDER_NAME);
       System.out.println("客户端启动");
        //2.循环给服务器写数据
       /* while (true){
            String result =service.test("收到请回答 !!"); //service.sayHello("are you ok !!");
            System.out.println(result);
            Thread.sleep(2000);
        }*/
        String result =service.sayHello(res); //发送数据到服务器端
        System.out.println("服务器端："+result);

        //参数类型
        Class<?>[] type=new Class[1];
        type[0]=String.class;
        rpcRequest.setParameterTypes(type);
        //参数
        String[] parameter=new String[1];
        parameter[0]="我是有参方法";
        rpcRequest.setParameters(parameter);//入参

        byte[] baBytes2=jsonSerializer.serialize(rpcRequest);
        String res2 = new String(baBytes2);
        String result2 =service.sayHello(res2); //发送数据到服务器端
        System.out.println("服务器端："+result2);


    }
}
